using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using System.Linq;
using Core.SimpleTemp.Repositories.Internal;
using Core.SimpleTemp.Entitys;
using System.Collections.Generic;
using Core.Toolkit.Core.Repositories;
using System.Data;
using Core.SimpleTemp.Application.UserApp;

namespace Core.SimpleTemp.Api.PC.Api
{
    /// <summary>
    /// PX_Plan
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class PX_TrainingArchivesController : CoreApiController
    {
        private CoreDBContext _coreDBContext;
        private SQLHelper _sqlHelper;
        private SysUserAppService _userService;
        /// <summary>
        /// PX_Plan
        ///</summary>
        public PX_TrainingArchivesController(CoreDBContext coreDBContext, SQLHelper sqlHelper,SysUserAppService userService)
        {
            _coreDBContext = coreDBContext;
            _sqlHelper = sqlHelper;
            this._userService = userService;
        }

        [HttpPost("GetList")]
        [PermissionFilter(nameof(PX_TrainingArchivesController) + "_" + nameof(PX_TrainingArchivesController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList(PagingModel<object> pagingQueryModel)
        {
            PageModel<object> result = new PageModel<object>();


            //获得当前登录用户所在的工厂SYFTODO

            try
            {
                dynamic model = pagingQueryModel.Model;
                string queryName = model.EmployeeName;

                Guid? department = model.Department;
                string queryWhereDepartment = "";
                if (department != null)
                {
                    string queryDepartment = department.ToString();
                    queryWhereDepartment = " and du.SysDepartmentID='" + queryDepartment + "'";
                }
                
                string sql = "Select ui.SysUserId Id,ui.Name,ui.JoinDate,ud.dutyid,du.name dutyName,du.SysDepartmentID,d.Name departmentName From SysUserInfor as ui left join " +
                    " (select SysUserId, min(SysDutyID) dutyid from SysUserDuty group by SysUserId) " +
                    " as ud on ui.SysUserID = ud.SysUserId " +
                    " left join SysDuty as du on du.id = ud.dutyid" +
                    " left join SysDepartment as d on du.SysDepartmentID = d.id" +
                    @" where ui.Name like '%" + queryName + "%'" +
                    queryWhereDepartment;
                    

                var data = _sqlHelper.ExecuteDataTable(sql, null);
                List<object> pageData = new List<object>();

                foreach (DataRow d in data.Rows)
                {
                    pageData.Add(new
                    {
                        id=d["Id"],
                        name = d["Name"],
                        joinDate = d["JoinDate"],
                        dutyName = d["dutyName"],
                        departmentName = d["departmentName"]
                    });
                }

                result.PageData = pageData.Skip(pagingQueryModel.Offset).Take(pagingQueryModel.Limit).ToList();
                result.RowCount = pageData.Count;
                return this.JsonSuccess(result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
            
        }


        [HttpGet("Get")]
        public async Task<IActionResult> Get(Guid id)
        {
            var user = await _userService.GetAsync(id);

            if (user != null)
            {
                var userInfor = user.SysUserInfor;
                var retObj = new
                {
                    employeeName = userInfor.Name,
                    gender = userInfor.Sex,
                    birthDate = userInfor.Birthday == null ? "" : ((DateTime)userInfor.Birthday).ToShortDateString(),
                    photoPath = userInfor.PhotoPath
                };
                return this.JsonSuccess(retObj);
            }
            else
            {
                return this.JsonFaild("该用户不存在");
            }

            
        }


    }
}
